//
//  LFNetworkTool.h
//  AntFarms
//
//  Created by LF on 2017/2/24.
//  Copyright © 2017年 GG. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AFNetworking.h>
#import "LFNetworkResult.h"
#import <ReactiveObjC/ReactiveObjC.h>

#define CUSTOM_ERROR_CODE           100000
#define ERROR_DOMIN                 @"请求错误"

#define Not_Login_Code CUSTOM_ERROR_CODE+1

#define MYError(ErrorInfo) [NSError errorWithDomain:ERROR_DOMIN code:CUSTOM_ERROR_CODE userInfo:@{NSLocalizedDescriptionKey:ErrorInfo}] //Error
#define CLARIFY_RESULT(RESULT) \
    NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];\
    if (RESULT.info && RESULT.info.length > 0) { \
        [userInfo setObject:RESULT.info forKey:NSLocalizedDescriptionKey];\
        NSError *error = [NSError errorWithDomain:ERROR_DOMIN code:CUSTOM_ERROR_CODE userInfo:userInfo];\
        [subscriber sendError:error]; \
    }

/**
 * 请求方式枚举
 */
typedef enum : NSUInteger {
    RequestTypeGET=0,
    RequestTypePOST,
} RequestType;

// 成功
typedef void (^HttpSuccessBlock)(id json);
// 失败
typedef void (^HttpFailureBlock)(NSError * error);
// 下载进度
typedef void (^HttpDownloadProgressBlock)(CGFloat progress);
// 上传进度
typedef void (^HttpUploadProgressBlock)(CGFloat progress);

@interface LFNetworkTool : NSObject

#pragma mark - 属性

//**  网络是否可用 */
@property (nonatomic, assign) BOOL isNetUsable;

//**  网络实际状态 */
@property (nonatomic, assign) AFNetworkReachabilityStatus netStatus;

@property (nonatomic, strong) RACSubject *netStatusChangeSubject; /**< 网络状态改变 */

#pragma mark - 方法

+ (LFNetworkTool *) shareInstance;
+ (void) lf_userCookie;
+ (void) lf_cleanCookies;


/**
 网络请求
 
 @param type 请求的类型
 @param urlS 主体URL
 @param parameters 参数
 @param success 成功结果回调
 @param failure 失败结果回调
 */
+ (void) httpRequest:(RequestType)type
                 url:(NSString *)urlS
                para:(NSDictionary *)parameters
             success:(HttpSuccessBlock)success
             failure:(HttpFailureBlock)failure;

/**
 *  下载文件
 *
 *  @param path     url路径
 *  @param success  下载成功
 *  @param failure  下载失败
 *  @param progress 下载进度
 */

+ (void)downloadWithPath:(NSString *)path
                 success:(HttpSuccessBlock)success
                 failure:(HttpFailureBlock)failure
                progress:(HttpDownloadProgressBlock)progress;

/**
 *  上传图片
 *
 *  @param path     url地址
 *  @param images    UIImage对象
 *  @param imagekey    imagekey
 *  @param params  上传参数
 *  @param success  上传成功
 *  @param failure  上传失败
 *  @param progress 上传进度
 */

+ (void)uploadImageWithPath:(NSString *)path
                     params:(NSDictionary *)params
                  thumbName:(NSString *)imagekey
                     images:(NSArray *)images
                    success:(HttpSuccessBlock)success
                    failure:(HttpFailureBlock)failure
                   progress:(HttpUploadProgressBlock)progress;

/**
 获取网络状态是否可用

 @param returnStatus 回调网络是否可用
 */
- (void) checkNetworkStatus:(void(^)(BOOL netUsable))returnStatus;


/**
 获取实际的网络状态

 @param resultBack 回调实际的网络状态
 */
- (void) getNetorkStatus:(void(^)(AFNetworkReachabilityStatus status))resultBack;



/**
 结束网络监听
 */
- (void)stopChetNet;


@end
